const Koa = require("koa");

const app = new Koa();

// 中间件：记录请求时间
app.use(async (ctx, next) => {
  const start = Date.now();
  await next(); // 调用下一个中间件
  const ms = Date.now() - start;
  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});

// 响应请求
app.use(async (ctx, next) => {
  ctx.body = "Hello Koa";
  next();
});
app.use(async (ctx, next) => {
  ctx.body += "\n";
  ctx.body += "Koa is awesome";
});

// 启动服务器
app.listen(3000, () => {
  console.log("Server running on http://localhost:3000");
});
